﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.K3.FIN.HS.App.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace RCE.K3.YGJD.App.Report
{
    [Description("存货收发存汇总表-添加字段")]
    [Kingdee.BOS.Util.HotUpdate]
    public class InOutStockSummaryServiceAddFieds: InOutStockSummaryService
    {

        private string[] TempTableName;
        //重写创建临时表方法
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            //创建临时表用于保存自己的数据
            IDBService dbservice = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();
            TempTableName = dbservice.CreateTemporaryTableName(this.Context, 1);
            string strTable = TempTableName[0];

            //调用基类方法，获取初步的查询结果
            base.BuilderReportSqlAndTempTable(filter, strTable);

            //对初步的查询结果处理，然后回填到默认的存放查询结果到临时表 
            StringBuilder sb = new StringBuilder();
            string strSql = @"/*dialect*/select t1.*,t2.FDeptID as FDeptID
                                into {0} from {1} t1 
                                left join t_bd_material  t2 on t2.FMATERIALID = t1.FMATERIALBASEID and t2.fuseorgid = t1.FSTOCKORGID";

            sb.AppendFormat(strSql, tableName, strTable);

            DBUtils.Execute(this.Context, sb.ToString());
        }


        public override void CloseReport()
        {
            //删除临时表
            if (TempTableName == null || TempTableName.Length == 0)
            {
                return;
            }

            IDBService dbservice = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();

            dbservice.DeleteTemporaryTableName(this.Context, TempTableName);

            base.CloseReport();
        }

    }
}
